package com.yfbao.web.gateway.router;

import com.yfbao.web.gateway.config.FastApiProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

@Component
public class RequestRouter {

    private final FastApiProperties fastApiProperties;
//    private final BusinessProperties businessProperties;
    private final AntPathMatcher pathMatcher = new AntPathMatcher();

    public RequestRouter(FastApiProperties fastApiProperties) {
        this.fastApiProperties = fastApiProperties;

    }

    /**
     * 判断请求是否需要代理到FastAPI
     */
    public boolean shouldProxyToFastApi(String path) {
        for (String pattern : fastApiProperties.getProxyPaths()) {
            if (pathMatcher.match(pattern, path)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为本地业务路径
     */
//    public boolean isLocalBusinessPath(String path) {
//        for (String pattern : businessProperties.getPaths()) {
//            if (pathMatcher.match(pattern, path)) {
//                return true;
//            }
//        }
//        return false;
//    }

    /**
     * 构建FastAPI完整URL
     */
    public String buildFastApiUrl(String path) {
        String baseUrl = fastApiProperties.getBaseUrl();
        if (baseUrl.endsWith("/")) {
            baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
        }
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return baseUrl + path;
    }
}
